﻿#include <QCoreApplication>
#include "circlefitsolver.h"
#include <iostream>

const double CENTER_X = 120;
const double CENTER_Y = 150;
const double RADIUS = 99;
const double PI = 3.14159;

double my_rand(double range)
{
    double x = static_cast<double> ( rand() ) / RAND_MAX;
    return range * (x * 2 - 1);
}

void test(int center_x, int center_y, double radius, double err)
{
    vector<POINT> points;
    for(int i = 0; i < 300; i++)
    {
        int x = center_x + radius * cos(i / 180.0 * M_PI) + my_rand(err);
        int y = center_y + radius * sin(i / 180.0 * M_PI) + my_rand(err);

        //cout << x << " " << y << endl;
        points.push_back(POINT(x, y));
    }
    double x, y ,r;
    CircleFitSolver solver;

    r = -1;     solver.Fit(points, x, y, r, CircleFitSolver::DIST_L2);
    cerr << "DIST_L2: Center = (" << x << ", " << y << "), Radius = " << r << endl;

    r = -1;     solver.Fit(points, x, y, r, CircleFitSolver::DIST_L12);
    cerr << "DIST_L12: Center = (" << x << ", " << y << "), Radius = " << r << endl;

    r = -1;     solver.Fit(points, x, y, r, CircleFitSolver::DIST_L1);
    cerr << "DIST_L1: Center = (" << x << ", " << y << "), Radius = " << r << endl;;

    r = -1;     solver.Fit(points, x, y, r, CircleFitSolver::DIST_FAIR);
    cerr << "DIST_FAIR: Center = (" << x << ", " << y << "), Radius = " << r << endl;

    r = -1;     solver.Fit(points, x, y, r, CircleFitSolver::DIST_WELSCH);
    cerr << "DIST_WELSCH: Center = (" << x << ", " << y << "), Radius = " << r << endl;

}
int main(int argc, char *argv[])
{
//    QCoreApplication a(argc, argv);
    test(100, 120, 40, 4);
//    return a.exec();
}
